#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

int S[100005];

struct node {
	int u, v;
	int w;
	node(int ui, int vi, int wi) {
		u = ui;
		v = vi;
		w = wi;
	}
};

vector<node> E;

int getf(int x) {
	if (S[x] == x) return x;
	return S[x] = getf(S[x]);
}

void merage(int x, int y) {
	int a = getf(x), b = getf(y);
	if (a != b) {
		S[a] = b;
		S[x] = b;
	}
}

bool cmp(node x, node y) {
	return x.w < y.w;
}

int main() {
	while (1) {
		int n, ai, bi, r;
		int wi;
		scanf("%d", &n);
		if (n == 0) break;
		scanf("%d", &r);
		for (int i = 0; i <= n; ++i) {
			S[i] = i;
		}
		for (int i = 1; i <= r;  ++i) {
			scanf("%d %d %d", &ai, &bi, &wi);
			E.push_back(node(ai, bi, wi));
		}
		sort(E.begin(), E.end(), cmp);
		int index = 1, np = 0;
		int ans = 0;
		while (index <= n - 1) {
			if (np >= E.size()) {
				//printf("Impossible\n")
				break;
			} 
			node now = E[np++];
			if (getf(now.u) == getf(now.v)) continue;
			++index;
			ans += now.w;
			merage(now.u, now.v);
		}
		/*
		for (int i = 2; i <= n; ++i) {
			if (getf(i) != getf(1)) {
				printf("Impossible\n");
				return 0;
			}
		}*/

		printf("%d\n", ans);
		E.clear();
	}
	return 0;
}
